/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rosedb.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;

/**
 *
 * @author riu
 */
public class Button extends JButton{

    private Image img;
    private int padding = 3;
    private boolean hover = false;
    private boolean action = false;

    public Button() {
        super();
        try {
            img = ImageIO.read(Button.class.getResource("/rosedb/resources/button.png"));
        } catch (IOException ex) {
            Logger.getLogger(Button.class.getName()).log(Level.SEVERE, null, ex);
        }

        addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                action = true;
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                action = false;
                repaint();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                hover = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                hover = false;
                repaint();
            }
        });

        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    @Override
    public void paint(Graphics g) {
        
        ((Graphics2D)g).setBackground(getParent().getBackground());
        g.clearRect(0, 0, getWidth(), getHeight());
        
        if(isEnabled()){
            g.setColor(hover ? action ? Color.white : Color.decode("#2288cc") : Color.lightGray);
        }else{
            g.setColor(Color.gray);
        }

        g.fillRoundRect(padding/2, padding/2, getWidth()-padding-1, getHeight()-padding-1, 20, 20);
        g.setColor(Color.BLACK);
        g.drawRoundRect(padding/2, padding/2, getWidth()-padding-1, getHeight()-padding-1, 20, 20);

        //supi

        if(isEnabled()){
            g.setColor(getForeground());
        }else{
            g.setColor(Color.darkGray);
        }
        g.drawString(getText(), getWidth()/2-g.getFontMetrics().stringWidth(getText())/2, getHeight()/2+g.getFont().getSize()/4+1);
    }





}
